# coding: utf-8


import time
import sys
import socket


def doRequest():
    sock = socket.socket()
    sock.connect(("www.baidu.com", 80))
    sock.send("GET / HTTP /1.1\r\nHost: www.baidu.com"
              "\r\nConnection: Close\r\n\r\n".encode("utf-8"))
    response = sock.recv(1024)
    return response


def main():
    start = time.time()
    for i in range(int(sys.argv[1])):
        doRequest()
    print("spend time: %s" % (time.time() - start))


if __name__ == "__main__":
    main()

"""
>>> python blocking_io.py 10
spend time: 0.6139998435974121

>>> python blocking_io.py 100
spend time: 6.358001470565796

因为socket是阻塞式的方式调用的，所以cpu执行到sock.connect(), 
sock.recv()，就会一直卡在那里直到socket的状态就绪，所以浪费了很多的CPU的时间。

"""




